package com.lin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.internal.$Gson$Preconditions;
import com.lin.mapper.SysRolesMapper;
import com.lin.pojo.*;
import com.lin.service.SysRolesService;
import com.lin.service.SysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysRolesServiceImpl extends ServiceImpl<SysRolesMapper,SysRoles> implements SysRolesService{

    @Autowired
    SysRolesMapper sysRolesMapper;

    //显示所以菜单
    @Override
    public SysRoles rolesbyMenus(SysUsers users,Integer id) {
            //组装数据据
        SysRoles roles=new SysRoles();
            roles.setSysRoles(sysRolesMapper.querybyroles(users));
            roles.setSysMenus(sysRolesMapper.querybyMenus(users,id));
        return roles;
    }

    //循环查找子菜单
    public List<SysMenus> querybymenus(SysUsers users,Integer id){

        List<SysMenus> list=sysRolesMapper.querybyMenus(users,id);

        if (list.size()>0  && list!=null){

            for (SysMenus sysMenus : list ){

                while (true){
                   List<SysMenus> list1= sysRolesMapper.querybyMenus(users,sysMenus.getId());
                    if (list1.size()>0 && list1!=null){
                        sysMenus.setSysMenus(list1);
                    }{break;}

                }
            }

        }

        for (SysMenus sys : list){
            System.out.println(sys);
        }


        return list;
    }

    @Override
    public Map addSysroles(SysRoles sysRoles) {
        boolean num = this.save(sysRoles);
        //int num = personMapper.insert(person);
        Map map =new HashMap();
        map.put("code","0");
        map.put("msg","添加失败");
        if(num){
            map.put("code","1");
            map.put("msg","添加成功");
        }
        return map;
    }

    @Override
    public Map delSysroles(int id) {
        boolean num = this.removeById(id);
        Map map =new HashMap();
        map.put("code","0");
        map.put("msg","删除失败");
        if(num){
            map.put("code","1");
            map.put("msg","删除成功");
        }
        return map;
    }

    @Override
    public Map upddateSysroles(SysRoles sysRoles) {
        boolean num = this.updateById(sysRoles);
        Map map =new HashMap();
        map.put("code","0");
        map.put("msg","修改失败");
        if(num){
            map.put("code","1");
            map.put("msg","修改成功");
        }
        return map;
    }

    @Override
    public PageVo<SysRoles> querySysrolesbycond(SysRoles sysRoles, int pageno, int pagesize) {
        //组装分页条件
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRoles> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRoles>(pageno,pagesize);
        //条件查询
        QueryWrapper<SysRoles> queryWrapper =new QueryWrapper<SysRoles>();
        if(StringUtils.isNotBlank(sysRoles.getName())){
            queryWrapper.like("NAME",sysRoles.getName());
        }
       /* if(person.getBirthday()!=null){
            //查询生日 大于当前指定日期的
            queryWrapper.gt("birthday",person.getBirthday());
        }*/
        Page<SysRoles> page1 = this.page(page,queryWrapper);
        PageVo<SysRoles> pageVo = new PageVo<SysRoles>();
        pageVo.setTotal(page1.getTotal());
        pageVo.setRows(page1.getRecords());
        return pageVo;
    }
}